// press.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Press } from './press.entity';
import { CreatePressDto } from './dto/create-press.dto';
import { RemovePressDto } from './dto/remove-press.dto';
@Injectable()
export class PressService {
  constructor(
    @InjectRepository(Press)
    private readonly pressRepository: Repository<Press>,
  ) {}
  async create(createPressDto: CreatePressDto): Promise<Press> {
    const { createdAt } = createPressDto;
    createPressDto.createdAt = createdAt || new Date();
    createPressDto.updatedAt = new Date();

    delete createPressDto.id;

    return await this.pressRepository.save(createPressDto);
  }
  // 列表
  async findAll(query: any): Promise<any> {
    const { keyword, category, page = 1, limit = 100 } = query;
    const skip = (page - 1) * limit;
    let params = {
      skip,
      take: limit,
    };
    params = Object.assign(params, {
      order: {
        sorting: 'ASC',
      },
    });
    const [data, total] = await this.pressRepository.findAndCount(params);
    return {
      total,
      data,
    };
  }
  // 删除
  async delete(removeProductCategoriesDto: RemovePressDto): Promise<any> {
    const { ids } = removeProductCategoriesDto;

    return this.pressRepository.delete(ids);
  }
  // 更新
  async update(updatePressData): Promise<any> {
    const { id, updatePressDto } = updatePressData;
    updatePressDto.updatedAt = new Date();

    return await this.pressRepository.update(id, updatePressDto);
  }

  // 根据ID查找
  async findOneById(id: string): Promise<any> {
    return this.pressRepository.createQueryBuilder().where([{ id }]).getOne();
  }
  // 其他操作，例如更新、删除等等...
}
